package bot;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

import administration.Masters;

import commands.Bandit;
import commands.Calc;
import commands.Command;
import commands.Crap;
import commands.Leave;
import commands.Lmod;
import commands.Quit;
import commands.Time;
import commands.Timer;

public class CommandBot extends LogBot {
	public static final String prefix = "!";
	HashMap<String, Command> commands = new HashMap<String, Command>();
	Masters master;

	public Masters getMaster() {
		return master;
	}

	public CommandBot() {
		super();
		initConfig();
		master = new Masters();
		loadStartMods();
	}

	private void initConfig() {
		try {
			this.setAutoNickChange(true);
			setName("ciibot");
			setVersion("ciibot");
			setFinger("Take your hands off me!");
			setLogin( "ciibot.owns");
			connect("irc.se.quakenet.org");
			joinChannel("#cii");
			/**
			Properties properties = new Properties();
			properties.load(new FileInputStream("config.ini"));
			setName(properties.getProperty("botname", "ciibot"));
			setName("Flamezzz_s");
			setVersion(properties.getProperty("botversion", "ciibot"));
			setFinger(properties.getProperty("botfinger",
					"Take your hands off me!"));
			setLogin(properties.getProperty("botlogin", "ciibot.owns"));
			connect(properties.getProperty("defaultserver",
					"irc.se.quakenet.org"));
			joinChannel(properties.getProperty("homechannel", "#cii"));*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onMessage(String channel, String sender, String login,
			String hostname, String message) {
		super.onMessage(channel, sender, login, hostname, message);
		if (message.startsWith(prefix)) {
			message = message.substring(1);
			for (Command command : commands.values()) {
				if (message.startsWith(command.getCommandName())) {
					if (command.execCommand(channel, sender, login, hostname,
							message.replaceFirst(command.getCommandName(), ""),
							this)) {
						return;
					}
				}
			}
		}
	}

	public void onPrivateMessage(String sender, String login, String hostname,
			String message) {
		super.onPrivateMessage(sender, login, hostname, message);
		if (message.startsWith(prefix)) {
			message = message.substring(1);
			for (Command command : commands.values()) {
				if (message.startsWith(command.getCommandName())) {
					if (command.execCommand(sender, login, hostname,
							message.replaceFirst(command.getCommandName(), ""),
							this)) {
						return;
					}
				}
			}
		}
	}

	public void onInvite(String targetNick, String sourceNick,
			String sourceLogin, String sourceHostname, String channel) {
		if (master.checkMaster(sourceLogin)) {
			System.out.println(channel);
			String[] list = channel.split(targetNick);
			this.joinChannel(list[list.length - 1]);
		}
	}

	@Override
	public void onDisconnect() {
		while (!isConnected()) {
			try {
				reconnect();
			}  catch (Exception e) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
				}
			}
		}
	}

	public HashMap<String, Command> getCommands() {
		return commands;
	}

	public static void main(String[] args) throws Exception {
		CommandBot bot = new CommandBot(); // Now start our bot up.
		bot.setVerbose(true); // Enable debugging output.
	}

	public void loadStartMods() {
		this.getCommands().put("Lmod", new Lmod());
		getCommands().put("Quit", new Quit());
		getCommands().put("Leave", new Leave());
		getCommands().put("Time", new Time());
		getCommands().put("Timer", new Timer());
		getCommands().put("Crap", new Crap());
		getCommands().put("Calc", new Calc());
		getCommands().put("Bandit", new Bandit());
	}
}
